package com.ai.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;


public class EncryptUtil {

	/**
	 * 加密字符串
	 * @param sSrc 加密前的字符串
	 * @param cKey 加密密钥
	 * @return 加密后的字符串
	 * @throws Exception 加密失败异常
	 */
	public static String Encrypt(String sSrc, String cKey) throws Exception {
		// 判断Key是否为16位
		if (cKey.length() != 16) {
			return null;
		}
		byte[] raw = cKey.getBytes("utf-8");
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
		// 此处使用BASE64做转码功能，同时能起到2次加密的作用。并且返回时替换掉换行符及特殊换行
		return Base64.getEncoder().encodeToString(encrypted).replaceAll("[\\s*\t\n\r]", "");
	}

	/**
	 * 解密字符串
	 * @param sSrc 加密后的字符串
 	 * @param cKey 加密密钥
	 * @return 解密后的字符串
	 * @throws Exception 解密失败异常
	 */
	public static String Decrypt(String sSrc, String cKey) throws Exception {
		// 判断Key是否为16位
		if (cKey.length() != 16) {
			return null;
		}
		byte[] raw = cKey.getBytes("utf-8");
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, skeySpec);
		byte[] encrypted1 = Base64.getDecoder().decode(sSrc);// 先用base64解密
		try {
			byte[] original = cipher.doFinal(encrypted1);
			String originalString = new String(original, "utf-8");
			return originalString;
		} catch (Exception e) {
			System.out.println(e.toString());
			return null;
		}
	}


	public static void main(String[] args) throws Exception {
		/*
		 * 此处使用AES-128-ECB加密模式，key需要为16位。
		 */
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String e = EncryptUtil.Encrypt("{\"account\":\"18996368488\",\"sysCode\":\"100003\",\"isValid\":false,\"reqTime\":\""+sdf.format(new Date())+"\"}\n", "1k014VI0s80h87nV");
		System.out.println("加密后字符串："+e);
		System.out.println("解密后字符串："+ EncryptUtil.Decrypt(e, "1k014VI0s80h87nV"));

//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//		System.out.println(sdf.format(new Date()));
//
//		String parsEncrypted1 = "{\"productCode\":\"qinlingli\",\"reqTime\":\""+sdf.format(new Date())+"\"}";
//		String e = EncryptUtil.Encrypt("{\"productCode\":\"qinlingli\",\"reqTime\":\""+sdf.format(new Date())+"\"}\n", "CSYEe0kpI0Mz4Dn1");
//		System.out.println(e);
////		URLDecoder.
//		System.out.println("转义后："+URLEncoder.encode(e));
//		String fzy = URLDecoder.decode(URLEncoder.encode(e));  //反转义
//		String u = EncryptUtil.Decrypt(fzy, "2RLhn2cpv1gtlDEO");
//		System.out.println(u);

//		String s = "XCMSnbnjyNAqpVhoXiMqrIDqjZ91tEPxPXfw2t8QtQFG08CgVjJzOu4g9pEwqDPdC9LwJLlcWgsYcjrzf6XMag==";
//		String u = EncryptUtil.Decrypt(s, "CSYEe0kpI0Mz4Dn1");
//		System.out.println(u);
//
//		JSONObject jsonData = JSONObject.parseObject(u);
//		System.out.println(jsonData);
//
//		String mobile = jsonData.getString("mobile");
//		String reqTime = jsonData.getString("reqTime");
//		System.out.println(mobile);
//		System.out.println(reqTime);
//
//
//		Date date = sdf.parse(reqTime);
//
//		System.out.println("date:" + date.getTime());
//
//		Date date1 = new Date(); // new Date()为获取当前系统时间
//
//		System.out.println("date1:" + date1.getTime());
//
//
//		if(date1.getTime()-date.getTime()>300000){
//			System.out.println(date1.getTime()-date.getTime());
//		}


//		String fzy = URLEncoder.encode("XCMSnbnjyNAqpVhoXiMqrIDqjZ91tEPxPXfw2t8QtQEFusYmGL5t0hv3Z5QN6+TWxmcN5TPnzne9HVS04Dq7lg==");  //反转义
//		String u = EncryptUtil.Decrypt("XCMSnbnjyNAqpVhoXiMqrIDqjZ91tEPxPXfw2t8QtQEFusYmGL5t0hv3Z5QN6+TWxmcN5TPnzne9HVS04Dq7lg==", "CSYEe0kpI0Mz4Dn1");
//		System.out.println(u);

	}

}
